﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;

namespace API.Controllers
{
    [RoutePrefix("api/CustomType")]
    public class CustomTypeController : ApiController
    {
        B_CustomType bc;
        public CustomTypeController()
        {
            bc = new B_CustomType();
        }
        /// <summary>
        /// 获取所有模板自定义分类（有分页的）
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAll")]
        public IHttpActionResult GetAll(int page, int size) {
            var data = from c in bc.GetAll()
                       select new CustomType
                       {
                           CTypeID = c.CTypeID,
                           CName = c.CName,
                           CPower = c.CPower,
                           IsShow = c.IsShow
                       };
            return Ok(new Result<List<CustomType>>()
            {
                Data = data.Skip((page - 1) * size).Take(size).ToList(),
                Msg = data.Count().ToString()
            });
        }
        /// <summary>
        /// 获取所有模板自定义分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAll")]
        public IHttpActionResult GetAll()
        {
            var data = from c in bc.GetAll()
                       select new Param_CustomType
                       {
                           CTypeID = c.CTypeID,
                           CName = c.CName,
                           CPower = c.CPower,
                           IsShow = c.IsShow
                       };
            return Ok(new Result<List<Param_CustomType>>()
            {
                Data = data.ToList()
            });
        }

        /// <summary>
        /// 添加自定义分类
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Add")]
        [HttpPost]
        public IHttpActionResult Add(CustomType custom) {
            try
            {
                bc.Add(custom);
                return Ok(new Result<object>());
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>()
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }
        }

        /// <summary>
        /// 删除自定义分类
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns></returns>
        [Route("Delete/{id}")]
        [HttpGet]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                if (new B_CustomDetails().GetAll().Count(x=>x.CTypeID==id)>0)
                {
                    return Ok(new Result<object>()
                    {
                        Code = 500,
                        Msg = "无法删除"
                    });
                }
                bc.Delete(id);
                return Ok(new Result<object>());
            }
            catch (Exception exp)
            {
                return Ok(new Result<object>()
                {
                    Code = 500,
                    Msg = exp.Message
                });
            }

        }

        /// <summary>
        /// 查询自定义分类详情
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns></returns>
        [Route("Find/{id}")]
        [HttpGet]
        public IHttpActionResult Find(int id) {
            try
            {
                var data = bc.FindById(id);
                if (data==null)
                {
                    return Ok(new Result<object> { Code=404,Msg= "数据为空" });
                }
                return Ok(new Result<object>()
                {
                    Code = 200,
                    Data = new Param_CustomType {
                        CTypeID= data.CTypeID,
                        CName= data.CName,
                        CPower= data.CPower,
                        IsShow= data.IsShow
                    },
                });
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>()
                {
                    Code = 500,
                    Data = ex.Message,
                });
            }
        }

        /// <summary>
        /// 修改自定义分类详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Update/{id}")]
        [HttpPost]
        public IHttpActionResult Update(int id,CustomType param)
        {
            try
            {
                var data = bc.FindById(id);
                if (data==null)
                {
                    return Ok(new Result<object>()
                    {
                        Code = 404,
                        Data = "数据为空"
                    });
                }
                if (bc.GetAll().Count(x => x.CName == param.CName) == 0 || data.CName == param.CName)
                {
                    bc.Update(id, param);
                    return Ok(new Result<object>());
                }
                else
                {
                    return Ok(new Result<object>()
                    {
                        Code = 500,
                        Data = "分类已存在"
                    });
                }
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>()
                {
                    Code = 500,
                    Data = ex.Message
                });
            }
        }

    }
}
